package com.cqupt.mislab.erpn.service.teacher.impl;

import com.cqupt.mislab.erpn.commons.utils.ResultUtil;
import com.cqupt.mislab.erpn.dao.teacher.TeacherDao;
import com.cqupt.mislab.erpn.dao.user.LoginUserDao;
import com.cqupt.mislab.erpn.dao.user.RegisterUserDao;
import com.cqupt.mislab.erpn.model.Result;
import com.cqupt.mislab.erpn.model.entity.user.BasicUser;
import com.cqupt.mislab.erpn.model.entity.user.RegisterUser;
import com.cqupt.mislab.erpn.model.po.TeacherPo;
import com.cqupt.mislab.erpn.service.teacher.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: com.cqupt.mislab.erpn.service
 * @description:
 * @author: keji0
 * @date: 2020/7/28 0:58
 */

@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private LoginUserDao loginUserDao;
    @Autowired
    private RegisterUserDao registerUserDao;

    @Override
    public Boolean importStudents(List<List<String>> infos,String adminId) {
        try {
            for (List<String> info:infos) {
                teacherDao.importApprovedUser(info);
                teacherDao.insertReferences(info.get(0),adminId);
            }
        } catch (Exception e) {
            log.info("插入记录失败");
            return false;
        }
        return true;
    }

    @Override
    public Boolean deleteBatchUsers(String[] ids) throws Exception {
        for (String id:ids) {
            if (!deleteApprovedUser(id)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean deleteApprovedUser(String userId) throws Exception {
        try {
            if(loginUserDao.deleteApprovedUser(userId)&&teacherDao.deleteReference(userId))
                return true;
        }catch (Exception  e){
            log.info("删除失败！");
            throw new Exception("操作失败");
        }
        return false;
    }

    @Override
    public List<BasicUser> rangeSearch(String keyword, int status) {
        List<BasicUser> users = teacherDao.findUsersUnclear(status, keyword);
        return users;
    }

    // TODO
    @Override
    public BasicUser exactSearch(String userId,int status) {
        BasicUser basicUser = teacherDao.findBasicUser(status, userId);
        if(basicUser != null) return basicUser;
        return null;
    }

    @Override
    public boolean resetPassword(int status, String userId) {
        BasicUser basicUser = exactSearch(userId, status);
        if(basicUser != null){
            if(status == 0)teacherDao.resetPasswordApproved(userId);
            if(status == 1)teacherDao.resetPasswordRegister(userId);
            return true;
        }
        return false;
    }

    @Override
    public Boolean teacherLogin(String teacherId, String password, HttpSession session) {
        TeacherPo teacherPo = teacherDao.judgeAccount(teacherId, password);
        if(teacherPo !=null){
            session.setAttribute("teacherPo",teacherPo);
            return true;
        }
        return false;
    }

    @Override
    public List pageUser(int page,int limit,int status) {
        Integer maxPage = countRecordsApprovedUser(limit);

        if(maxPage <= 0||page>maxPage||page<0){
            log.info("教师端查询页数超越界限！");
            return null;
        }
        int start = (page - 1)*limit;
        if(status == 0) {
            return teacherDao.pageFindApprovedUser(start, limit);
        }
        if(status == 1) {
            return   teacherDao.pageFindRegisterUser(start, limit);
        }
        return null;
    }

    @Override
    public List getAllUser(int status) {
        if(status == 0){
            return teacherDao.findAllApprovedUser();
        }
        else{
            return  teacherDao.findAllRegister();
        }
    }

//    @Override
//    public List<RegisterUser> pageRegisterUser(int page,int limit) {
//        int maxPage = countRecordsRegisterUser(limit);
//
//        if(page>maxPage||page<0){
//            log.info("教师端查询页数超越界限！");
//            return null;
//        }
//        int start = (page - 1)*limit;
//        List<RegisterUser> registerUsers = teacherDao.pageFindRegisterUser(start,limit);
//        return registerUsers;
//    }

    @Override
    public Integer countRecordsApprovedUser(int limit) {
        int records = teacherDao.countRecordsApprovedUser();
//        if(limit > records)return null;
        return records/limit+1;
    }

    @Override
    public Integer countRecordsRegisterUser(int limit) {
        int records = teacherDao.countRecordsApprovedUser();
        if(limit > records)return null;
        return records/limit+1;
    }

    @Override
    public Result findAllRegister() {
        return ResultUtil.success(teacherDao.findAllRegister());
    }

    @Override
    public Result findAllApprovedUser() {
        return ResultUtil.success(teacherDao.findAllApprovedUser());
    }

    @Override
    public Result passRegisterUser(String userId, String adminId) {
        RegisterUser registerUser = registerUserDao.findRegister(userId);
        loginUserDao.addApprovedUser(registerUser);
        teacherDao.insertReferences(userId,adminId);
        registerUserDao.deleteRegisterByUserId(userId);
        return ResultUtil.success();
    }

    @Override
    public Result refuseRegisterUser(String userId, String adminId) {
        registerUserDao.deleteRegisterByUserId(userId);
        return ResultUtil.success();
    }


    @Override
    public Result passBatchRegisterUsers(String[] userIds, String adminId) {
        for (int i = 0; i < userIds.length; i++) {
            passRegisterUser(userIds[i],adminId);
        }
        return ResultUtil.success();
    }

    @Override
    public Result refuseBatchRegisterUsers(String[] userIds, String adminId) {
        for (int i = 0; i < userIds.length; i++) {
            refuseRegisterUser(userIds[i],adminId);
        }
        return ResultUtil.success();
    }
}
